package com.feimt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feimt.dto.DishDto;
import com.feimt.mapper.DishMapper;
import com.feimt.pojo.Dish;
import com.feimt.pojo.DishFlavor;
import com.feimt.service.DishFlavorService;
import com.feimt.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /*
    *存菜品和口味
    * */
    @Transactional
    @Override
    public void saveDishAndFlavors(DishDto dishDto) {
        /*1.保存菜品基本信息*/
        this.save(dishDto);
        /*2.保存菜品口味信息*/
        /*2.1 口味 多个*/
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (int i = 0; i < flavors.size(); i++) {
            DishFlavor flavor = flavors.get(i);
            /*2.2 口味 dish_id*/
            flavor.setDishId(dishDto.getId());
        }
        /*2.3 保存口味 批量*/
        dishFlavorService.saveBatch(flavors);

    }
}
